import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

// Custom APIs for renderer
const api = {
  // PDF相关API
  selectPdfFile: () => ipcRenderer.invoke('select-pdf-file'),
  readPdfFile: (filePath) => ipcRenderer.invoke('read-pdf-file', filePath),
  addQRCodeToPdf: (params) => ipcRenderer.invoke('add-qr-code-to-pdf', params),
  selectSaveDirectory: () => ipcRenderer.invoke('select-save-directory'),
  savePdfFile: (params) => ipcRenderer.invoke('save-pdf-file', params)
}
const attach = {
  selectFile: () => ipcRenderer.invoke('select-file'),
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
    contextBridge.exposeInMainWorld('attach', attach)
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
  window.api = api
  window.attach = attach
}
